package loading;

import gui.GUI;

import javax.imageio.*;
import java.io.*;
import java.awt.image.*;
import java.awt.*;
import java.awt.geom.*;

/*
 * The ImageLoader is used to load and store and image. Calling draw
 * and passing in appropriate variables will allow the image to
 * be drawn to a graphics object
 */
public class ImageLoader 
{
	private String imgPath;
	private BufferedImage img;
	private double degrees;
	
	public ImageLoader(String path)
	{
		loadImage(path);
		degrees = 0;
	}
	public ImageLoader(String path, double degrees)
	{
		loadImage(path);
		this.degrees = degrees;
	}
	//creates a blank image of size x by y
	public ImageLoader(int x, int y)
	{
		img = new BufferedImage(x, y, BufferedImage.TYPE_4BYTE_ABGR);
		degrees = 0;
		
	}
	//loads the image based on the path
	public void loadImage(String path)
	{
		imgPath = path;
		try
		{
			img = ImageIO.read(new File(imgPath));
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
	}
	public BufferedImage getImage()
	{
		return img;
	}
	//draws and transforms an image
	public void draw(Graphics g, AffineTransform at)
	{
		Graphics2D g2d = (Graphics2D)g;
		at.concatenate(AffineTransform.getRotateInstance(degrees, img.getWidth()/2, img.getHeight()/2));
		g2d.drawImage(img, at, GUI.getInstance());
	}
	
	/**
     * @return the width of the image
     */
    public int width()
    {
        return getImage().getWidth();
    }
    
    /**
     * @return the height of the image
     */
    public int height()
    {
        return getImage().getHeight();
    }
    
    /**
     * @return the file name of the object
     */
    public String getFilename()
    {
        return imgPath;
    }
    /**
     * 
     * @param img1 the bottom image
     * @param img2 the top image
     * @return the combination of the two
     */
    public static ImageLoader add(ImageLoader img1, ImageLoader img2)
    {
    	ImageLoader image = new ImageLoader(img1.getImage().getWidth(), img1.getImage().getHeight());
    	image.getImage().getGraphics().drawImage(img1.getImage(), 0, 0, null);
    	image.getImage().getGraphics().drawImage(img2.getImage(), 0, 0, null);
    	return image;
    }
}

